package com.janj.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/ajaxServlet")
public class UserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //解决请求和响应乱码问题
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        //1 获取用户名username
        String username = req.getParameter("username");
        try {
            Thread.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //2 如果用户名是tom或者是jack就表示被占用了，否则就没有被占用
        if("tom".equals(username) || "jack".equals(username)){
            //占用了 //3 响应对应结果
            resp.getWriter().write("<font color='red'>用户名，已经被注册！</font>");
        }else{
            //可用 //3 响应对应结果
            resp.getWriter().write("<font color='green'>用户名可用！</font>");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
